import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MockCallerid {
    static SimpleDateFormat date = new SimpleDateFormat("MMdd");
    static SimpleDateFormat time = new SimpleDateFormat("HHmm");
    static SimpleDateFormat second = new SimpleDateFormat("ssSSS");
    static SimpleDateFormat sss = new SimpleDateFormat("ssSSS");

    public static void main(String[] args) {
        Date now=new Date();
        if(args.length>1){
            client(args[0],args[1]);
        }else {
            client("hubin"+sss.format(now),"181156"+second.format(now));
        }

    }

    private static void client(String name,String number) {
        Map<String,String> map=new HashMap<>();
        Date now=new Date();
        map.put("DATE",date.format(now));
        map.put("TIME",time.format(now));
        map.put("NAME",name);
        map.put("NMBR",number);
        map.put("MOCK","true");

        StringBuffer mesgBuffer=new StringBuffer();
        for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
            mesgBuffer.append(stringStringEntry.getKey()).append("=").append(stringStringEntry.getValue()).append("\n");
        }
        byte[] buf=mesgBuffer.toString().getBytes();
        try {
            DatagramSocket socket = new DatagramSocket(9024);
            DatagramPacket packet = new DatagramPacket(buf, buf.length,
                    InetAddress.getByName("localhost"),3520);
            socket.send(packet);
            System.out.println("telephone call success!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
